﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace PluginInterFace.DAL
{
    public class ssq_pick
    {
        public ssq_pick()
        {
            
        }
        public DataSet GetList(string strWhere)
        {
            AccessHelp AH = new AccessHelp();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT ssq_pick.期号, ssq_pick.号码, ssq.红1, ssq.红2, ssq.红3, ssq.红4, ssq.红5, ssq.红6, ssq.蓝");
            strSql.Append(" FROM ssq_pick ");
            strSql.Append("LEFT JOIN ssq ");
            strSql.Append("ON ssq_pick.期号=ssq.期号");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" ORDER BY ssq_pick.期号 DESC");
            return AH.GetDataSet(strSql.ToString());
        }

        public DataSet GetList(string strWhere, int sRecord, int mRecord)
        {
            AccessHelp AH = new AccessHelp();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT ssq_pick.期号, ssq_pick.号码, ssq.红1, ssq.红2, ssq.红3, ssq.红4, ssq.红5, ssq.红6, ssq.蓝");
            strSql.Append(" FROM ssq_pick ");
            strSql.Append("LEFT JOIN ssq ");
            strSql.Append("ON ssq_pick.期号=ssq.期号");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" ORDER BY ssq_pick.期号 DESC");
            return AH.GetDataSet(strSql.ToString(), sRecord, mRecord);
        }

        public bool AddData(Model.ssq_pick[] mss)
        {
            AccessHelp AH = new AccessHelp();
            StringBuilder strSql = new StringBuilder();
            string str;
            int i;
            for (i = 0; i < mss.Length; i++)
            {
                if(mss[i].号码=="" || mss[i].号码==null)
                    continue;
                str = string.Format("INSERT INTO ssq_pick VALUES ({0},'{1}')", mss[i].期号, mss[i].号码);
                if (!AH.ExecuteDataBase(str.ToString()))
                    return false;
            }
            return true;
        }

        public bool DelData(int 期号,string 号码)
        {
            AccessHelp AH = new AccessHelp();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("DELETE FROM ssq_pick WHERE 期号=" + 期号 + " and 号码='" + 号码 + "'");
            return AH.ExecuteDataBase(strSql.ToString());
        }
        //////////////////////////////
        static public List<int> GetRandomArray(string str, int count)
        {
            List<int> list = new List<int>();//保存取出的随机数
            string[] array = str.Split(',');//定义初始数组
            if(count>array.Length)
                return list;
            Random rnd = new Random();
            for (int j = 0; j < count; j++)
            {
                int index = rnd.Next(j, array.Length);//生成一个随机数，作为数组下标  
                int temp = int.Parse(array[index]);//从数组中取出index为下标的数  
                list.Add(temp);//将取出的数添加到list中  
                array[index] = array[j];//将下标为j的数交换到index位置  
                array[j] = temp.ToString();//将取出的数交换到j的位置  
            }
            return list;
        }

        //选择排序
        public void Sort(List<int> list)
        {
            for (int i = 1; i < list.Count; i++)
            {
                int t = list[i];
                int j = i;
                while ((j > 0) && (list[j - 1] > t))
                {
                    list[j] = list[j - 1];
                    --j;
                }

                list[j] = t;
            }
        }

        //阶乘
        public static double frac(int n)
        {
            if (n == 1)
                return 1;
            else
                return n * frac(n - 1);
        }

        public static int betCount(int n)
        {
            return (int)(frac(n) / (frac(n - 6) * frac(6)));
        }
    }
}
